/**
 * 权限控制器
 * 作者：GodMainCode
 * 创建时间：2024-01-17
 * 修改时间：2024-01-17
 * 修改人：GodMainCode
 */

import { 
  Controller, 
  Get, 
  Post, 
  Body, 
  Patch, 
  Param, 
  Delete,
  HttpCode,
  HttpStatus,
  UseGuards
} from '@nestjs/common';
import { 
  ApiTags, 
  ApiOperation, 
  ApiResponse,
  ApiParam,
  ApiBody
} from '@nestjs/swagger';
import { PermissionService } from './permission.service';
import { CreatePermissionDto } from './dto/create-permission.dto';
import { UpdatePermissionDto } from './dto/update-permission.dto';
import { Permission } from './entities/permission.entity';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RequirePermissions } from '../auth/decorators/permissions.decorator';

@ApiTags('权限管理')
@UseGuards(JwtAuthGuard)
@Controller('permission')
export class PermissionController {
  constructor(private readonly permissionService: PermissionService) {}

  @Post()
  @HttpCode(HttpStatus.CREATED)
  @RequirePermissions('permission:create')
  @ApiOperation({ summary: '创建权限' })
  @ApiBody({ type: CreatePermissionDto })
  @ApiResponse({ 
    status: HttpStatus.CREATED, 
    description: '权限创建成功',
    type: Permission 
  })
  create(@Body() createPermissionDto: CreatePermissionDto) {
    return this.permissionService.create(createPermissionDto);
  }

  @Get('tree')
  @RequirePermissions('permission:read')
  @ApiOperation({ summary: '获取权限树' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '获取权限树成功',
    type: [Permission] 
  })
  findTree() {
    return this.permissionService.findTree();
  }

  @Get()
  @RequirePermissions('permission:read')
  @ApiOperation({ summary: '获取所有权限' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '获取所有权限成功',
    type: [Permission] 
  })
  findAll() {
    return this.permissionService.findAll();
  }

  @Get(':id')
  @RequirePermissions('permission:read')
  @ApiOperation({ summary: '获取权限详情' })
  @ApiParam({ name: 'id', description: '权限ID' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '获取权限详情成功',
    type: Permission 
  })
  findOne(@Param('id') id: string) {
    return this.permissionService.findOne(+id);
  }

  @Patch(':id')
  @RequirePermissions('permission:update')
  @ApiOperation({ summary: '更新权限' })
  @ApiParam({ name: 'id', description: '权限ID' })
  @ApiBody({ type: UpdatePermissionDto })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '权限更新成功',
    type: Permission 
  })
  update(@Param('id') id: string, @Body() updatePermissionDto: UpdatePermissionDto) {
    return this.permissionService.update(+id, updatePermissionDto);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  @RequirePermissions('permission:delete')
  @ApiOperation({ summary: '删除权限' })
  @ApiParam({ name: 'id', description: '权限ID' })
  @ApiResponse({ 
    status: HttpStatus.NO_CONTENT, 
    description: '权限删除成功' 
  })
  remove(@Param('id') id: string) {
    return this.permissionService.remove(+id);
  }
} 